<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">货位编码：</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="code" class="layui-input" type="text" placeholder="请输入货位编码" />
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">仓库：</label>
                                    <div class="layui-input-inline mr0">
                                        <select id="warehouseId" name="warehouseId" lay-filter="warehouseId" lay-search
                                            lay-verify=""></select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">库区：</label>
                                    <div class="layui-input-inline mr0">
                                        <select id="warehouseAreaId" name="warehouseAreaId" lay-filter="warehouseAreaId"
                                            lay-search lay-verify=""></select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">货架：</label>
                                    <div class="layui-input-inline mr0">
                                        <select id="shelfId" name="shelfId" lay-search lay-verify=""></select>
                                    </div>
                                </div>
                            </div>
                            <div class="query-rt">
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog"
                                        lay-submit>查询
                                    </button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="warehouseShelfFlatBtnAdd"
                                class="layui-btn icon-btn color-reseda">添加平仓库位</button>
                            <!--                            <button id="warehouseShelfBtnAdd" class="layui-btn icon-btn color-reseda">添加立体库位</button>-->
                        </div>
                        <div class="lay-btn-rt">
                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="warehouseShelfSpaceTable" lay-filter="warehouseShelfSpaceTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="warehouseShelfSpaceTableBar">
    <a class="layui-btn layui-btn-xs" lay-event="printBarcode">打印条码</a>
    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit">编辑</a>
</script>

<script type="text/html" id="warehouseShelfFlatForm">
    <form lay-filter="warehouseShelfFlatForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <div class="layui-col-xs6">
                <label class="layui-form-label">仓库</label>
                <div class="layui-input-block">
                    <select id="warehouseFlatId" name="warehouseId" lay-filter="warehouseFlatId" lay-search lay-verify="required"></select>
                </div>
            </div>

            <div class="layui-col-xs6">
                <label class="layui-form-label">库区</label>
                <div class="layui-input-block">
                    <select id="warehouseAreaFlatId" name="warehouseAreaId" lay-filter="warehouseAreaFlatId" lay-search lay-verify="required"></select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-col-xs6">
                <label class="layui-form-label">初始编码</label>
                <div class="layui-input-block">
                    <input name="firstCode" placeholder="请输入初始编码" type="text" class="layui-input"
                           lay-verify="required"
                           required/>
                </div>
            </div>
            <div class="layui-col-xs6">
                <label class="layui-form-label">库位数</label>
                <div class="layui-input-block">
                    <input name="spaceNum" placeholder="请输入库位数" type="text" class="layui-input"
                           lay-verify="required|isOnlySn"
                           required/>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-col-xs6">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    <select name="status">
                        <option value="enable">启用</option>
                        <option value="unable">停用</option>
                    </select>
                </div>
            </div>
            <div class="layui-col-xs6">
                <label class="layui-form-label">排序</label>
                <div class="layui-input-block">
                    <input name="sortNumber" placeholder="请输入排序" type="text" class="layui-input"
                           lay-verify="sortNumber"
                           required/>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注描述</label>
            <div class="layui-input-block">
                <textarea name="description" type="text" class="layui-input" style="height: 100px;"/>
            </div>
        </div>

        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="warehouseShelfFlatFormSubmit" type="button" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin', 'element'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var element = layui.element;


        //仓库列表
        admin.initDataSelect("warehouse/warehouse/admin/administration/list", { limit: 9999 }, "warehouseId", null, "仓库", null, null);
        //仓库选择事件
        form.on('select(warehouseId)', function (data) {
            $("#warehouseAreaId").empty();
            //仓库库区列表
            admin.initDataSelect("warehouse/warehouse/admin/warehouseFactoryArea/list", {
                limit: 9999,
                warehouseId: data.value
            }, "warehouseAreaId", null, "库区", null, null);
        });
        //仓库库区选择事件
        form.on('select(warehouseAreaId)', function (data) {
            $("#shelfId").empty();
            //库区货架列表
            admin.initDataSelect("warehouse/warehouse/admin/warehouseFactoryShelf/list", {
                limit: 9999,
                warehouseAreaId: data.value
            }, "shelfId", null, "库区货架", null, null);
        });

        // 渲染表格
        var insTb = table.render({
            elem: '#warehouseShelfSpaceTable',
            url: config.base + 'warehouse/warehouse/admin/warehouseFactoryShelfSpace/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                { type: 'numbers', title: '序号', width: 80 }
                , { field: 'code', align: 'center', title: '货位编码' }
                , { field: 'warehouseName', align: 'center', title: '仓库' }
                , { field: 'warehouseAreaName', align: 'center', title: '库区' }
                , { field: 'shelfName', align: 'center', title: '货架' }
                , { field: 'description', align: 'center', title: '备注' }
                , { field: 'createTime', align: 'center', title: '创建时间', width: 180 }
                , { align: 'center', toolbar: '#warehouseShelfSpaceTableBar', title: '操作', width: 200 }
            ]]
        });

        // 工具条点击事件
        table.on('tool(warehouseShelfSpaceTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'printBarcode') { // 打印
                admin.getPrintBarcode('warehouse/warehouse/admin/warehousePalletBoxList/getBarCode', data.code);
            } else if (obj.event === 'edit') { // 编辑
                showFlatEditModel(data);
            }
        });

        //监听排序
        table.on('sort(warehouseShelfSpaceTable)', function (obj) {
            table.reload('warehouseShelfSpaceTable', {
                initSort: obj,
                where: {
                    sort: obj.field,
                    order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({ where: data.field, page: { curr: 1 } }, 'data');
        });


        // 添加按钮点击事件
        $('#warehouseShelfFlatBtnAdd').click(function () {
            showFlatEditModel();
        });

        function showFlatEditModel(data) {
            admin.open({
                type: 1,
                area: '860px',
                offset: ['120px', '550px'],
                title: data ? '修改仓库货架' : '添加仓库货架',
                content: $('#warehouseShelfFlatForm').html(),
                success: function (layero, index) {

                    console.log(data,"data")
                    // 如果是编辑模式，填充表单数据
                    if (data) {
                        form.val('warehouseShelfFlatForm', {
                            id: data.id,
                            warehouseId: data.warehouseId,
                            warehouseAreaId: data.warehouseAreaId,
                            firstCode: data.code,
                            spaceNum: 1, // 根据实际需求修改
                            status: data.status,
                            sortNumber: data.sortNumber,
                            description: data.description
                        });

                        // 手动触发仓库选择事件，加载库区数据
                        form.on('select(warehouseFlatId)', function (data) {
                            console.log(1111111)
                            // $("#warehouseAreaFlatId").empty();
                            //仓库库区列表
                            admin.initDataSelect("warehouse/warehouse/admin/warehouseFactoryArea/list", {
                                limit: 9999,
                                warehouseId: data.value
                            }, "warehouseAreaFlatId", data.warehouseAreaId, "库区", null, null);
                        });

                        admin.initDataSelect("warehouse/warehouse/admin/warehouseFactoryArea/list", {
                            limit: 9999,
                            warehouseId: data.warehouseId
                        }, "warehouseAreaFlatId", data.warehouseAreaId, "库区", null, null);

                        // 触发仓库选择
                        // $("#warehouseFlatId").trigger('change');
                    }

                    //仓库列表
                    admin.initDataSelect("warehouse/warehouse/admin/administration/list", { limit: 9999 }, "warehouseFlatId", data ? data.warehouseId : null, "仓库", null, null);
                    // 初始化库区选择器

                    form.on('select(warehouseFlatId)', function (data) {
                        // $("#warehouseAreaFlatId").empty();
                        //仓库库区列表
                        admin.initDataSelect("warehouse/warehouse/admin/warehouseFactoryArea/list", {
                            limit: 9999,
                            warehouseId: data.value
                        }, "warehouseAreaFlatId", data.warehouseAreaId, "库区", null, null);
                    });


                    // 触发仓库选择
                    $("#warehouseAreaFlatId").trigger('change');
                    //仓库选择事件
                    // form.on('select(warehouseFlatId)', function (data) {
                    //     $("#warehouseAreaFlatId").empty();
                    //     //仓库库区列表
                    //     admin.initDataSelect("warehouse/warehouse/admin/warehouseFactoryArea/list", {limit: 9999,warehouseId: data.value}, "warehouseAreaFlatId", null, "库区", null, null);
                    // });
                    // 表单提交事件
                    form.on('submit(warehouseShelfFlatFormSubmit)', function (d) {
                        layer.load(2);
                        var url = data ? 'warehouse/warehouse/admin/warehouseFactoryShelfSpace/update' : 'warehouse/warehouse/admin/warehouseFactoryShelfSpace/addFlat';
                        admin.req(url, d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, { icon: 1 });
                                table.reload('warehouseShelfSpaceTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, { icon: 2 });
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 重置搜索
        form.on('submit(resets)', function (data) {
            $(".lay-header").find('select').val("");
            $(".lay-header").find('input').val("");
            let field = data.field;
            for (let key in field) {
                field[key] = ''
            }
            insTb.reload({ where: field, page: { curr: 1 } }, 'data');
        });
    });
</script>